import { mapGetters } from "vuex";
import PolylineTrailMaterialProperty3 from "@/views/tongyongnn/ThreeDimensional/components/zhuoseqi/caizhi";
let mixin = {
  props: {},
  computed: {},
  data() {
    return {};
  },
  mounted() {},
  methods: {
    loadguangzhui(position) {
      let lon = (position[0] * 180.0) / Math.PI;
      let lat = (position[1] * 180.0) / Math.PI;

      var scene = this.$root.viewer.scene;
      var viewer = this.$root.viewer;
      // 1 雷达位置计算
      // 1.1 雷达的高度
      // 1.2 地面位置(垂直地面)
      var positionOnEllipsoid = Cesium.Cartesian3.fromDegrees(lon, lat);
      // 1.3 中心位置
      // 1.4 顶部位置(卫星位置)
      // 1.5 矩阵计算
      var modelMatrix = Cesium.Matrix4.multiplyByTranslation(
        Cesium.Transforms.eastNorthUpToFixedFrame(positionOnEllipsoid),
        new Cesium.Cartesian3(0.0, 0.0, position[2] * 0.5),
        new Cesium.Matrix4()
      );
      // 2 相机飞入特定位置
      // 3 创建卫星
      // 4 创建雷达放射波
      // 4.1 先创建Geometry
      var cylinderGeometry = new Cesium.CylinderGeometry({
        length: position[2],
        topRadius: 0.0,
        bottomRadius: position[2] * 0.5,
        // vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
        vertexFormat:
          Cesium.MaterialAppearance.MaterialSupport.TEXTURED.vertexFormat
      });
      // 4.2 创建GeometryInstance
      var redCone = new Cesium.GeometryInstance({
        geometry: cylinderGeometry,
        modelMatrix: modelMatrix,
        id: "circle"
        // attributes : {
        //     color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.RED)
        // }
      });

      if (this.scaninstance) {
        // console.log("555555");
        // console.log(this.scaninstance);
        // this.scaninstance.modelMatrix = modelMatrix;
        // this.scaninstance.geometryInstances[0].geometry = cylinderGeometry;
        // this.scaninstance.geometryInstances[0].modelMatrix = modelMatrix;
        // this.scaninstance.geometryInstances = []
        // this.scaninstance.geometryInstances = cylinderGeometry;
        // var attributes = this.scaninstance.getGeometryInstanceAttributes("circle");

        // console.log(attributes);
        this.scaninstance.destroy();
        this.addscan1(redCone, scene, viewer);
      } else {
        this.addscan1(redCone, scene, viewer);
      }
    },
    addscan1(redCone, scene, viewer) {
      var radar = scene.primitives.add(
        new Cesium.Primitive({
          geometryInstances: [redCone],
          // appearance : new Cesium.PerInstanceColorAppearance({
          //     closed : true,
          //     translucent: false
          // })
          releaseGeometryInstances: false,
          appearance: new Cesium.MaterialAppearance({
            // 贴图像纹理
            // material: Cesium.Material.fromType('Image', {
            //     image: './images/earth.png'
            // }),
            // 贴棋盘纹理
            // material: Cesium.Material.fromType('Checkerboard'),
            // 自定义纹理
            material: new Cesium.Material({
              fabric: {
                type: "VtxfShader1",
                uniforms: {
                  color: new Cesium.Color(0.2, 1.0, 0.0, 1.0),
                  repeat: 30.0,
                  offset: 0.0,
                  thickness: 0.3
                },
                source: `
                  uniform vec4 color;
                  uniform float repeat;
                  uniform float offset;
                  uniform float thickness;
                  czm_material czm_getMaterial(czm_materialInput materialInput)
                  {
                      czm_material material = czm_getDefaultMaterial(materialInput);
                      float sp = 1.0/repeat;
                      vec2 st = materialInput.st;
                      float dis = distance(st, vec2(0.5));
                      float m = mod(dis + offset, sp);
                      float a = step(sp*(1.0-thickness), m);
                      material.diffuse = color.rgb;
                      material.alpha = a * color.a;
                      return material;
                  }
                  `
              },
              translucent: false
            }),
            faceForward: false, // 当绘制的三角面片法向不能朝向视点时，自动翻转法向，从而避免法向计算后发黑等问题
            closed: true // 是否为封闭体，实际上执行的是是否进行背面裁剪
          })
        })
      );
      this.scaninstance = radar;
      // 5 动态修改雷达材质中的offset变量，从而实现动态效果。
      viewer.scene.preUpdate.addEventListener(function() {
        var offset = radar.appearance.material.uniforms.offset;
        offset -= 0.001;
        if (offset > 1.0) {
          offset = 0.0;
        }
        radar.appearance.material.uniforms.offset = offset;
      });
    },
    loadguangzhui1(position) {
      const data = {
        circle: [
          0.0005,
          (position[0] * 180.0) / Math.PI,
          (position[1] * 180.0) / Math.PI,
          20
        ], // 第一个参数 0.003表示半径，第二个第三个分别表示底座圆心的坐标,第四个表示切割成多少个点。组成多少个面。越多会越卡 尽量实际项目不影响效果，越少越好。
        observer: [
          (position[0] * 180.0) / Math.PI,
          (position[1] * 180.0) / Math.PI,
          position[2]
        ], //观察点，也就是光源点
        material: Cesium.Color.RED.withAlpha(0.9), //光的材质
        number: 1000 //数字越小速度越快
      };
      if (!this.aaa) {
        this.ddd = data;
        this.aaa = this.addcan(data);
      } else {
        function f(entity, arr, X0, Y0) {
          entity.polygon.hierarchy = new Cesium.CallbackProperty(function() {
            //回调函数
            return new Cesium.PolygonHierarchy(
              Cesium.Cartesian3.fromDegreesArrayHeights([
                data.observer[0],
                data.observer[1],
                data.observer[2], //观察点
                arr[0] + X0,
                arr[1] + Y0,
                0,
                arr[2] + X0,
                arr[3] + Y0,
                0
              ])
            );
          }, false);
        }

        this.aaa.map((item, i) => {
          if (i === this.aaa.length - 1) {
            f(
              item,
              [
                this.point[i].x,
                this.point[i].y,
                this.point[0].x,
                this.point[0].y
              ],
              (position[0] * 180.0) / Math.PI - this.ddd.circle[1],
              (position[1] * 180.0) / Math.PI - this.ddd.circle[2]
            );
          } else {
            f(
              item,
              [
                this.point[i].x,
                this.point[i].y,
                this.point[i + 1].x,
                this.point[i + 1].y
              ],
              (position[0] * 180.0) / Math.PI - this.ddd.circle[1],
              (position[1] * 180.0) / Math.PI - this.ddd.circle[2]
            );
          }
        });
      }
    },
    addcan(data) {
      let _this = this;
      /**
       * 添加自定义灯光扫描;
       */
      /*
       * 求圆周上等分点的坐标
       * ox,oy为圆心坐标
       * r为半径
       * count为等分个数
       */
      function createLightScan_getCirclePoints(r, ox, oy, count) {
        let point = []; //结果
        let radians = (Math.PI / 180) * Math.round(360 / count), //弧度
          i = 0;
        for (; i < count; i++) {
          let x = ox + r * Math.sin(radians * i),
            y = oy + r * Math.cos(radians * i);
          point.unshift({ x: x, y: y }); //为保持数据顺时针
        }
        return point;
      }
      //生成 entityCList面--形成圆锥
      function createLightScan_entityCList(viewer, point, data) {
        const lon = data.observer[0],
          lat = data.observer[1],
          h = data.observer[2];
        const entityCList = [];
        //创建 面
        for (let i = 0; i < point.length; i++) {
          let hierarchy;
          if (i === point.length - 1) {
            hierarchy = new Cesium.PolygonHierarchy(
              Cesium.Cartesian3.fromDegreesArrayHeights([
                lon,
                lat,
                h,
                point[i].x,
                point[i].y,
                0,
                point[0].x,
                point[0].y,
                0
              ])
            );
          } else {
            hierarchy = new Cesium.PolygonHierarchy(
              Cesium.Cartesian3.fromDegreesArrayHeights([
                lon,
                lat,
                h,
                point[i].x,
                point[i].y,
                0,
                point[i + 1].x,
                point[i + 1].y,
                0
              ])
            );
          }

          let material = new PolylineTrailMaterialProperty3(
            Cesium.Color.fromRandom({
              alpha: 3.0
            }),
            5000,
            "/configuration/3d/t/color52.png",
            true,
            1
          );

          const entityC = _this.$root.viewer.entities.add({
            name: "三角形",
            polygon: {
              hierarchy: hierarchy,
              outline: false,
              perPositionHeight: true, //允许三角形使用点的高度
              material: material
              // material: data.material,
            }
          });
          entityCList.push(entityC);
        }

        return entityCList;
      }

      //生成分割点
      const point = createLightScan_getCirclePoints(
        data.circle[0],
        data.circle[1],
        data.circle[2],
        data.circle[3]
      );
      this.point = point;
      //生成 entityCList 圆锥
      const entityCList = createLightScan_entityCList(this.viewer, point, data);
      // for (let i = 0; i < entityCList.length; i++) {
      //   if (i !== entityCList.length - 1) {
      //     createLightScan_changeOnePosition(data, entityCList[i], [
      //       point[i].x,
      //       point[i].y,
      //       point[i + 1].x,
      //       point[i + 1].y,
      //     ]); //中间arr 代表的是点的坐标
      //   } else {
      //     createLightScan_changeOnePosition(data, entityCList[i], [
      //       point[i].x,
      //       point[i].y,
      //       point[0].x,
      //       point[0].y,
      //     ]);
      //   }
      // }
      return entityCList;
    }
  }
};

export default mixin;
